home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2817 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.5 KB  |  57 lines

  1. Path: atglab.bls.com!Alun.Champion
  2. From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Parameterless functions
  5. Date: 19 Jan 1996 22:22:09 GMT
  6. Organization: Computer People Inc.
  7. Message-ID: <ALUN.CHAMPION.96Jan19172209@g7240065.bridge.bst.bls.com>
  8. References: <9601191115.aa06760@paris.ics.uci.edu>
  9. NNTP-Posting-Host: bstfirewall.bst.bls.com
  10. In-reply-to: klefstad@catalina.ICS.UCI.EDU's message of 19 Jan 96 19:18:31 GMT
  11.  
  12. In article <9601191115.aa06760@paris.ics.uci.edu> klefstad@catalina.ICS.UCI.EDU ("Raymond Klefstad, Ph.D.") writes:
  13.  
  14. : Why does the following function work?  Is this just gcc or is it correct
  15. : C++?  endl and ends are implemented this way.
  16.  
  17. : #include <iostream.h>
  18.  
  19. : ostream& newline(ostream& out)
  20. : {
  21. :     return out << "Hello";
  22. : }
  23.  
  24. : main()
  25. : {
  26. :     cout << "Hello" << newline;
  27. : }
  28.  
  29. This works because the ostream has an operator << defined that
  30. takes a function of the type above and all it does is call the function
  31. with itself as the argument i.e:
  32.  
  33.   ostream& operator << (ostream& (*)(ostream&)
  34.   { return (*f)(*this); }
  35.  
  36. Though the (*f) can be replaces with just f:
  37.  
  38.   ostream& operator << (ostream& (*)(ostream&)
  39.   { return f(*this); }
  40.   
  41. : Also, I noticed the following in some of the standard headers for gcc:
  42.  
  43. : int foo(int x) return y
  44. : {
  45. :     y = 10;
  46. :     // do stuff and drop of the end of the function, but y is returned
  47. :     // similar to Pascal.  Is this valid C++ or just GNU extensions.
  48. : }
  49.  
  50. Not any valid C++ I have seen before.
  51.  
  52. Regards
  53.  
  54.    -A.
  55. -- 
  56. | A.Champion                |
  57.